//
//  YTMineController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/9.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTMineController.h"
#import "UIViewController+YTNavBarController.h"
#import "YTMCollectionController.h"
#import "YTFollowAuthorController.h"
#import "YTInvitFriendsController.h"
#import "YTBallViewController.h"

@interface YTMineController () <UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableViewCell *navCell;  // 收藏cell
@property (weak, nonatomic) IBOutlet UITableViewCell *inviteCell; // 邀请好友cell
@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UIImageView *jijiangIamgeView;
@property (weak, nonatomic) IBOutlet UIImageView *isSignImgView;

@property (weak, nonatomic) IBOutlet UILabel *signTipLabel;

@end

@implementation YTMineController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YTMineController" bundle:nil];
        self = [sb instantiateInitialViewController];
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadUseInfo];

    [self setupUI];
    


    // 注册程序退到后台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationEnterBackground)
                                                 name: UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(showPushDetailVC:)
                                               name:kMinekPushNotificationName
                                             object:nil];
    
    self.jijiangIamgeView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jijiangIamgeViewClick)];
    [self.jijiangIamgeView addGestureRecognizer:tap];
    
    
}


#pragma mark: - 即将开放图片
- (void)jijiangIamgeViewClick
{
    if (!YTNetworkManager.shareManager.account.auth) {
        YTLoginViewController *logVC = [[YTLoginViewController alloc] init];
        [self.navigationController pushViewController:logVC animated:true];
        return;
    }
    // FIXME：- 记得去掉注释再上线
    if (!YTNetworkManager.shareManager.account.isPass.boolValue) {
        return;
    }
    
    [self performSegueWithIdentifier:@"go2EtaiBall" sender:nil];
}

#pragma mark: - 推送消息通知
- (void)showPushDetailVC:(NSNotification *)n
{
    BOOL isArticlePush = [n.userInfo[@"push_isArticle_type"] boolValue];
    NSString *ID = n.userInfo[@"news_id"];
    UIViewController *detailVC = isArticlePush ? [[YTHomeDetailController alloc] init] : [YTDetailNewController controller];
    isArticlePush ? [(YTHomeDetailController *)detailVC setID:ID] : [(YTDetailNewController *)detailVC setID:ID];
    [self.navigationController pushViewController:detailVC animated:true];
}



- (void)applicationEnterBackground
{
    if ([UIViewController isCurrentViewControllerVisible:self]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:kMinekPushNotificationName forKey:YTPushNotificationName];

    }
    
}

- (void)dealloc
{
    [NSNotificationCenter.defaultCenter removeObserver:self
                                                  name:UIApplicationDidEnterBackgroundNotification
                                                object:nil];
}




- (void)setupUI
{
    //setShadow
    self.navCell.layer.masksToBounds = false;
    self.navCell.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    self.navCell.layer.shadowOffset = CGSizeMake(0, 1);
    self.navCell.layer.shadowOpacity = 0.5;
    self.navCell.layer.shadowRadius = 5;
    
    self.navigationItem.title = @"";
//    UIImage *image = [UIImage imageNamed:@"icon_logo"];
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage dh_setNavImageName:@"icon_logo"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:nil action:nil];
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"icon_navbar_bgImage"] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    
    // 设置右边设置按钮
    UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_shezhe"]
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self action:@selector(setButtonClick)];
    
    self.navigationItem.rightBarButtonItem = setButton;
    
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:133.0f/255.0f
                                                                         green:142.0f/255.0f
                                                                          blue:143.0f/255.0f alpha:1]];
    
 
   // [self.navigationController.navigationBar setBackgroundColor:UIColor.whiteColor];
    [self.navigationController.navigationBar setBarTintColor:UIColor.whiteColor];
 
}

- (void)setButtonClick
{
    [self performSegueWithIdentifier:@"go2SetttingVC" sender:nil];
}

#pragma mark: - 关注按钮 监听
- (IBAction)followButtonClick
{
    // 判断是否登录
    if (!YTNetworkManager.shareManager.account.auth) {
        [self pushLoginVC];
        return;
    }
    
    YTFollowAuthorController *vc = [[YTFollowAuthorController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark: - 历史按钮 监听
- (IBAction)historyButtonClick:(UIButton *)sender
{
    // 判断是否登录
    if (!YTNetworkManager.shareManager.account.auth) {
        [self pushLoginVC];
        return;
    }
    
    
    YTMCollectionController *collectionVC = [[YTMCollectionController alloc] init];
    collectionVC.hidesBottomBarWhenPushed = YES;
    collectionVC.isHistory = YES;
    [self.navigationController pushViewController:collectionVC animated:true];
}

#pragma mark: - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 关于我们
    if ((indexPath.row == 2 && indexPath.section == 1) || (indexPath.row == 0 && indexPath.section == 1)) {
        return;
    }
    // 判断是否登录
    if (!YTNetworkManager.shareManager.account.auth) {
        [self pushLoginVC];
        return;
    }
    
    
    
    // 编辑用户信息
    if (indexPath.row == 0 && indexPath.section == 0) {
        [self performSegueWithIdentifier:@"go2EditInfo" sender:nil];
    }
    
    // 挖矿签到
    if (indexPath.row == 3 && indexPath.section == 0) {
        [self performSegueWithIdentifier:@"go2SignIn" sender:nil];
    }
    
    // 意见反馈
    if (indexPath.row == 1 && indexPath.section == 1) {
        [self performSegueWithIdentifier:@"go2Feedback" sender:nil];
    }
    
    // 邀请好友
    if (indexPath.row == 4 && indexPath.section == 0) {
        YTInvitFriendsController *inviteVC = [[YTInvitFriendsController alloc] init];
        [self.navigationController pushViewController:inviteVC animated:true];
    }
}

#pragma mark: - 用户是否登陆
- (void)pushLoginVC
{
    YTLoginViewController *vc = [[YTLoginViewController alloc] init];
        
    [self.navigationController pushViewController:vc animated:true];
        
}

#pragma mark: - 收藏按钮 监听
- (IBAction)collectionButtonClick:(UIButton *)sender
{
    // 判断是否登录
    if (!YTNetworkManager.shareManager.account.auth)
    {
        YTLoginViewController *loginVC = [[YTLoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:true];
        return;
    }
    
    YTMCollectionController *collectionVC = [[YTMCollectionController alloc] init];
    collectionVC.isHistory = NO;
    [self.navigationController pushViewController:collectionVC animated:true];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
 
    [self setupData];
    [self.navigationController.navigationBar  setTitleTextAttributes:@{
                                                                       NSForegroundColorAttributeName:[UIColor blackColor],
                                                                       NSFontAttributeName : [UIFont systemFontOfSize:15]
                                                                       }];
    [self.navigationItem.leftBarButtonItem setTintColor:kBackButtonColor];
    
    self.navigationController.navigationBar.subviews[0].subviews[0].hidden = YES;
   
    [self loadUseInfo];
    
}

#pragma mark: - 设置数据
- (void)setupData
{
    // 未登录状态
    if (!YTNetworkManager.shareManager.account.auth) {
        self.iconImgView.image = [UIImage imageNamed:@"icon_wode"];
        self.nameLabel.text = @"登录挖矿";
        self.detailLabel.text = @"已有100万人加入";
        self.isSignImgView.hidden = YES;
    }else{
        [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:kUserAccount.user_avatar]];
        self.nameLabel.text = YTNetworkManager.shareManager.account.display_name;
        self.detailLabel.text = YTNetworkManager.shareManager.account.decs;
        self.isSignImgView.hidden = YTNetworkManager.shareManager.account.isSign.boolValue;
        
    }
    
    if (YTNetworkManager.shareManager.account.isPass.boolValue) {
        self.jijiangIamgeView.image = [UIImage imageNamed:@"icon_bigImage"];
        self.signTipLabel.hidden = false;
    }
   
}

- (void)loadUseInfo
{
    [YTNetworkManager.shareManager requestUserInfoWithCompletion:^(BOOL isSucc, NSString *statusInfo) {
        if (!isSucc) {
            //[self.view showTipWithTipStr:statusInfo];
            return ;
        }
        
        
        
        [YTNetworkManager.shareManager requestUserIsSiginWithCompletion:^(BOOL isSucc, BOOL isSign)
         {
             if (!isSucc) {
                 // 什么都不做
             }
             YTNetworkManager.shareManager.account.isSign = [NSNumber numberWithBool:isSign];
             self.isSignImgView.hidden = YTNetworkManager.shareManager.account.isSign.boolValue;
             
             
             
         }];
        
        [self setupData];
    }];
 
    
    if (!YTNetworkManager.shareManager.account.isPass.boolValue) {
        [YTNetworkManager.shareManager requestIsPassCompletion:^(BOOL isSucc, BOOL isPass) {
            
            if (isSucc) {
               YTNetworkManager.shareManager.account.isPass = [NSNumber numberWithBool:isPass];
                if (YTNetworkManager.shareManager.account.isPass.boolValue) {
                    self.jijiangIamgeView.image = [UIImage imageNamed:@"icon_bigImage"];
                    self.signTipLabel.hidden = false;
                }
            }
            
        }];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

}

@end
